import {PACS} from "@/config/config";

import dcmjs from "dcmjs";

async function getInstanceUIDS(seriesUID) {
    const getInstanceURL = `${PACS.root}/rs/instances?SeriesInstanceUID=${seriesUID}`
    const res = await fetch(getInstanceURL)
    const data = await res.json()

    return data.map(instance => instance['00080018'].Value[0])
}

async function instanceUID_2_imageId(studyUID, seriesUID) {
    const sopInstanceUIDs =await getInstanceUIDS(seriesUID)
    return sopInstanceUIDs.map((uid) => {
        return `wadouri:${PACS.wadoURIRoot}?requestType=WADO&studyUID=${studyUID}&seriesUID=${seriesUID}&contentType=application%2Fdicom&objectUID=${uid}`;
    })
}

async function seriesImg2ImageIds(studyUID, seriesUID) {
    const imgIds =  await instanceUID_2_imageId(studyUID, seriesUID)
    const sopInstanceUID = '1.2.840.10008.5.1.4.1.1.2.20230724.105506.93192.1189'
    const url = `http://localhost:8080/dcm4chee-arc/aets/DCM4CHEE/rs/instances?SOPInstanceUID=${sopInstanceUID}&includefield=00180060&includefield=00181151`
    fetch(url)
        .then(response => {
            if (response.ok) {
                return response.json(); // 如果响应正常，返回 JSON 格式的 DICOM 元数据
            } else {
                throw new Error('获取 DICOM 标签信息出错');
            }
        })
        .then(data => {
            // 处理获取的 DICOM 标签信息，提取 KV 和 MA 信息
            console.log(data)
            const kv = data[0]['00180060']; // 获取 KV 信息
            const ma = data[0]['00181151']; // 获取 MA 信息
        })
        .catch(error => {
            console.error('获取 DICOM 标签信息出错:', error);
        });
    return imgIds
}

export {
    seriesImg2ImageIds
}